<template>
  <div class="container">
    <div class="mb10">
      <el-text class="mx-1"> 查询条件：</el-text>
    </div>
    <el-row :gutter="10">
      <el-col :xl="6" :lg="6" :md="12" :sm="24">
        <el-input v-model.trim="searchCondition.order_sn" placeholder="xxxx订单号" clearable></el-input>
      </el-col>
      <el-col :xl="6" :lg="6" :md="12" :sm="24">
        <el-input v-model.trim="searchCondition.out_bill_no" placeholder="商户订单号" clearable></el-input>
      </el-col>
      <el-col :xl="6" :lg="6" :md="12" :sm="24">
        <el-select v-model.trim="searchCondition.refund_source" placeholder="请选择退款单来源" clearable>
          <el-option v-for="item in orderSource" :label="item.dictLabel" :value="item.dictValue" :key="item.id"></el-option>
        </el-select>
      </el-col>
      <el-col :xl="6" :lg="6" :md="12" :sm="24">
        <el-select v-model.trim="searchCondition.refund_status" placeholder="请选择退款状态" clearable>
          <el-option v-for="item in orderStatus" :label="item.dictLabel" :value="item.dictValue" :key="item.id"></el-option>
        </el-select>
      </el-col>
      <el-col :xl="6" :lg="6" :md="12" :sm="24">
        <el-space wrap>
          <el-button type="success" :icon="Search" :disabled="disabledbtn" @click="searchInfo">搜索</el-button>
          <el-button type="warning" :icon="Refresh" :disabled="disabledbtn" @click="resetSearch">重置</el-button>
        </el-space>
      </el-col>
    </el-row>
    <div class="mb10">
      <el-text class="mx-1"> 查询结果：</el-text>
    </div>
    <el-table :data="tableList" border class="table" row-key="id" ref="multipleTableRef" v-loading="loading" :reserve-selection="true" @row-dblclick="rowDblclick">
      <el-table-column type="index" align="center"> </el-table-column>
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column prop="bill.order_sn" label="xxxx订单号" align="center"> </el-table-column>
      <el-table-column prop="out_order_no" label="商户订单号" align="center"> </el-table-column>
      <el-table-column prop="bill.title" label="账单标题" align="center"> </el-table-column>
      <el-table-column prop="refund_source_name" label="退款发起方" align="center"> </el-table-column>
      <el-table-column prop="create_time" label="申请退款时间" align="center" :formatter="(row: IRefundBillInfo) => handleVerifyFormatDate(row.create_time)"> </el-table-column>
      <el-table-column prop="refund_total_amount" label="退款金额/元" align="center"> </el-table-column>
      <el-table-column prop="refund_status" label="退款状态" align="center">
        <template #default="scope">
          <div class="w-tag">
            <el-tag :type="scope.row.refund_status === 'SUCCESS' ? 'success' : scope.row.refund_status === 'PROCESSING' ? 'warning' : 'danger'">{{ scope.row.refund_status_name }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="refund_time" label="退款时间" align="center" :formatter="(row: IRefundBillInfo) => handleVerifyFormatDate(row.refund_time)"> </el-table-column>
      <el-table-column label="操作" width="210" align="center">
        <template #default="scope">
          <div class="control-flex">
            <div v-if="scope.row.refund_status === 'PROCESSING'">
              <el-popconfirm placement="top" width="160" title="你确定要退款吗?" @confirm="handleAgreeRefund(scope.row)">
                <template #reference>
                  <el-button link :icon="EditPen" type="success">同意退款</el-button>
                </template>
              </el-popconfirm>
            </div>
            <div v-if="scope.row.refund_status === 'PROCESSING'">
              <el-popconfirm placement="top" width="160" title="你确定要拒绝退款吗?" @confirm="handleDenyRefund(scope.row)">
                <template #reference>
                  <el-button link :icon="Close" type="danger">拒绝退款</el-button>
                </template>
              </el-popconfirm>
            </div>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        @update:current-page="handleCurrentChange"
        @update:page-size="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper"
        :page-size="searchCondition.size"
        :current-page="searchCondition.current"
        :page-sizes="[10, 20, 30, 40, 50, 100]"
        :total="total"
        :hide-on-single-page="false">
      </el-pagination>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Close, EditPen, Search, Refresh } from "@element-plus/icons-vue";
import { handleVerifyFormatDate } from "@/utils/utils";
import { useState } from "@/hooks/useState.ts";
import type { IRefundBillInfo, ISearchRefundBillInfo } from "@/types/rentMachineManage/refundBillInfo";
import { useDictData } from "@/hooks/network/useDictData";
import type { IDictData } from "@/types/dict";
import { getDouyinRefundOrderInfo, updateDouyinRefundOrderAgreeRefundInfo, updateDouyinRefundOrderDenyRefundInfo } from "@/utils/api/douyinRefundOrder";
let { getDictData } = useDictData();
const { disabledbtn, loading, toggleState } = useState();
/* 退款状态 */
const orderStatus = ref<Array<IDictData>>([]);
/* 退款单来源 */
const orderSource = ref<Array<IDictData>>([]);
/* 暴露一个方法给父组件使用 */
defineExpose({
  /* 这一步关键，在setup语法糖中使用时 */
  getTableInfo
});
const multipleTableRef = useTemplateRef("multipleTableRef"); /* 列表组件的引用 */

const total = ref(0);
const searchCondition = reactive<ISearchRefundBillInfo>({
  order_sn: "",
  out_bill_no: "",
  refund_status: "",
  refund_source: "",
  size: 20,
  current: 1
});
const tableList = ref<Array<IRefundBillInfo>>([]);

onMounted(() => {
  getTableInfo();
});
/* 搜索 */
function searchInfo() {
  searchCondition.current = 1;
  searchCondition.size = 20;
  getTableInfo();
}

/**
 * 重置搜索内容
 */
function resetSearch() {
  searchCondition.order_sn = "";
  searchCondition.out_bill_no = "";
  searchCondition.refund_status = "";
  searchCondition.refund_source = "";

  searchCondition.current = 1;
  searchCondition.size = 20;

  getTableInfo();
}

/* 请求：获取表格中数据 */
function getTableInfo() {
  toggleState(true);
  getDouyinRefundOrderInfo<IRefundBillInfo, ISearchRefundBillInfo>(searchCondition)
    .then(res => {
      tableList.value = res.result.data;
      total.value = res.result.total;
    })
    .finally(() => {
      toggleState(false);
    });
}

/* 显示第几页 */
function handleCurrentChange(val: number) {
  searchCondition.current = val;
  getTableInfo();
}

/* 每页显示几条 */
function handleSizeChange(val: number) {
  searchCondition.current = 1;
  searchCondition.size = val;
  getTableInfo();
}

/* 双击当前行 */
function rowDblclick(row: IRefundBillInfo) {
  multipleTableRef.value!.toggleRowSelection(row);
}
/* 同意退款 */
function handleAgreeRefund(row: IRefundBillInfo) {
  toggleState(true);
  updateDouyinRefundOrderAgreeRefundInfo({
    id: row.id
  })
    .then(res => {
      ElMessage.success(res.msg);
      handleChangeRefundStatus(row.id, "SUCCESS");
    })
    .catch(() => {})
    .finally(() => {
      toggleState(false);
    });
}
/* 拒绝退款 */
function handleDenyRefund(row: IRefundBillInfo) {
  toggleState(true);
  updateDouyinRefundOrderDenyRefundInfo({
    id: row.id
  })
    .then(res => {
      ElMessage.success(res.msg);
      handleChangeRefundStatus(row.id, "FAIL");
    })
    .catch(() => {})
    .finally(() => {
      toggleState(false);
    });
}
/* 修改订单状态 */
function handleChangeRefundStatus(id: number, refundStatus: string) {
  const item = tableList.value.find(order => order.id === id);
  if (!item) return;
  item.refund_status = refundStatus;
  const status = orderStatus.value.find(item => item.dictValue === refundStatus);
  if (!status) return;
  item.refund_status_name = status.dictLabel;
}

getDictData("DOUYIN_REFUND_ORDER_STATUS", orderStatus);
getDictData("DOUYIN_REFUND_ORDER_SOURCE", orderSource);
</script>

<style lang="scss" scoped>
/* layout-布局 */
.el-col {
  border-radius: 4px;
}

.mb10 {
  margin-bottom: 10px;
}
/* 关键：列表右侧操作按钮的对其 */
:deep(.el-space__item) {
  line-height: 15px;
}

.el-button.is-text + .el-button.is-text {
  margin-left: 0;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}

.table {
  width: 100%;
  font-size: 14px;
}
.textAlignCenter {
  text-align: center;
}

/* 确定按钮的显示与隐藏 */
.displaybtn {
  display: none;
}

/* 提示图标设置 */
.icon-wzg-config {
  height: 30px;
  line-height: 30px;
}

.w-tag .el-tag {
  margin: 2px 5px;
}
</style>
